package com.fs.pandaFund.common.util;

import com.alibaba.druid.filter.config.ConfigTools;
import com.alibaba.druid.util.DruidPasswordCallback;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.util.Properties;

@Slf4j
public class DBPasswordCallback extends DruidPasswordCallback {
  //生成的公钥
  static final String PUBLIC_KEY = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJP/4bRrugp8JveGiNi1WmylnaRRDUQhyeMZIA1s5OZ6Rl+74jvS25u94mVkXRkQ8u64/9ulHdqhhpPuZBK4sR8CAwEAAQ==";


  @Override
  public void setProperties(Properties properties) {
    super.setProperties(properties);
    //获取配置文件中的已经加密的密码
    String pwd = (String)properties.get("password");
    if (!StringUtils.isEmpty(pwd)) {
      try {
        //这里的代码是将密码进行解密，并设置
        String password = ConfigTools.decrypt(PUBLIC_KEY, pwd);
        setPassword(password.toCharArray());
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
}